package org.iccs.san.repositories.sesame;

import org.openrdf.model.vocabulary.*;
import org.openrdf.repository.*;
import org.openrdf.repository.http.HTTPRepository;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
import org.openrdf.sail.memory.MemoryStore;
import org.openrdf.rio.*;

public class SesameRDFRepository {

	Repository therepository= null; 
	
	//useful -local- constants
	static RDFFormat NTRIPLES = RDFFormat.NTRIPLES;
	static RDFFormat N3 = RDFFormat.N3;
	static RDFFormat RDFXML = RDFFormat.RDFXML;
	static String RDFTYPE =  RDF.TYPE.toString();
	
	/**
	 *  In memory Sesame repository without type inferencing
	 */
	public SesameRDFRepository(){
		this(false);
	}
	
	/**
	 * In memory Sesame Repository with optional inferencing
	 * @param inferencing
	 */
	public SesameRDFRepository(boolean inferencing){
		try {
			if (inferencing){
			therepository = new  SailRepository(new ForwardChainingRDFSInferencer(new MemoryStore()));
				
			}else{
				therepository = new SailRepository(new MemoryStore());
			}
			therepository.initialize();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Connect to remote repository
	 * @param  sesameServer eg. http://localhost:8080/openrdf-sesame
	 * @param  repositoryID
	 */
	public SesameRDFRepository(String sesameServer,String repositoryID) {
		try { 
			therepository = new HTTPRepository(sesameServer, repositoryID);
			therepository.initialize();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}
	
	public Repository getRepository() {
		return this.therepository;
	}
	
	public void setNamespace(String prefix, String name) {
		try {
			RepositoryConnection con = therepository.getConnection();
			try {
				con.setNamespace(prefix, name);
			} finally {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();		
		}		
	}
		
}


